package br.com.q7solucoes.parsercinemaq7.tests;


import br.com.q7solucoes.parsercinemaq7.ServerConfigurationFileWrapper;
import br.com.q7solucoes.parsercinemaq7.URLParser;
import br.com.q7solucoes.parsercinemaq7.URLParserThread;
import br.com.q7solucoes.commons.utils.StringUtils;
import br.com.q7solucoes.parsercinemaq7.web.URLReader;
import br.com.q7solucoes.commons.wordpress.WPPost;
import br.com.q7solucoes.commons.wordpress.WPPostDAO;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author elenilson
 */
public class TestesElenilson {
    public static void main(String[] args) throws IOException, SQLException, ClassNotFoundException {
        testeCharsets();
    }

    private static void testeParser() throws IOException, ClassNotFoundException, SQLException {
        URLParser uRLParser = new URLParser(URLParser.URLType.MANAIRA_SHOPPING_CINEMA);
        WPPost result = uRLParser.parse();
        System.out.println(">> " + result.getContent());
        //testeAddPost(result);
    }

    private static void testeConverterString() throws UnsupportedEncodingException, IOException {
        String s = URLReader.readURL(URLParser.MAG_SHOPPING_URL);
        String convertida = StringUtils.convertIsoToUTF8(s);

        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        System.out.println(convertida);

        convertida = StringUtils.convertUTF8ToIso(convertida);

        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        System.out.println(convertida);
    }

    private static void testeBuildDate() {
        System.out.println(">> " + StringUtils.buildCineDate());
    }

    private static void testeAddPost(WPPost post) throws IOException, ClassNotFoundException, SQLException {
        if(post == null){
            List<String>  tags = new ArrayList<String>();
            List<String>  categories = new ArrayList<String>();

            categories.add(URLParser.CINEMA_CATEGORY);

                post = new WPPost("teste de elenilson 11", "conteudo 6 ééé sããão", URLParser.MAG_SHOPPING_IMAGE_URL,
                        GregorianCalendar.getInstance().getTime(), categories, tags);
        }
        
        String hostMysqlDatabase = ServerConfigurationFileWrapper.getInstance().getMysqlDatabaseHost();
        String mysqlDatabase = ServerConfigurationFileWrapper.getInstance().getMysqlDatabase();
        String mysqlDatabaseCode = ServerConfigurationFileWrapper.getInstance().getMysqlDatabaseCode();
        String userMysqlDatabase = ServerConfigurationFileWrapper.getInstance().getMysqlDatabaseUser();
        String passwordMysqlDatabase = ServerConfigurationFileWrapper.getInstance().getMysqlDatabasePassword();
        
        WPPostDAO dao = new WPPostDAO(mysqlDatabase, hostMysqlDatabase, userMysqlDatabase, passwordMysqlDatabase, mysqlDatabaseCode);
        
        int result = dao.add(post);
        
        System.out.println(">> " + result);
    }

    private static void testeFormat() {
        System.out.println(">> " + String.format("SELECT id FROM wp_%s_posts WHERE guid LIKE '%%%s%%'", "lala", "lele"));
    }

    private static void testeConvertDate() {
        System.out.println(">> " +
        StringUtils.converteDateToString(GregorianCalendar.getInstance().getTime()));
    }

    private static void testeURLParserThread() throws SQLException, ClassNotFoundException, IOException {
        String hostMysqlDatabase = ServerConfigurationFileWrapper.getInstance().getMysqlDatabaseHost();
        String mysqlDatabase = ServerConfigurationFileWrapper.getInstance().getMysqlDatabase();
        String mysqlDatabaseCode = ServerConfigurationFileWrapper.getInstance().getMysqlDatabaseCode();
        String userMysqlDatabase = ServerConfigurationFileWrapper.getInstance().getMysqlDatabaseUser();
        String passwordMysqlDatabase = ServerConfigurationFileWrapper.getInstance().getMysqlDatabasePassword();
        
        WPPostDAO dao = new WPPostDAO(mysqlDatabase, hostMysqlDatabase, userMysqlDatabase, passwordMysqlDatabase, mysqlDatabaseCode);
        
        URLParserThread uRLParserThread = new URLParserThread(URLParser.URLType.MANAIRA_SHOPPING_CINEMA, dao, true);
        uRLParserThread.run();
    }

    private static void testeConvertUTF8() throws UnsupportedEncodingException {
        String iso = StringUtils.convertIsoToUTF8("ãçeééíu");
        System.out.println(">> iso: " + iso);
        
        String utf8 = StringUtils.convertIsoToUTF8(iso);
        
        System.out.println(">> utf8: " + utf8);
    }

    private static void testeCharsets() {
        for (String string : Charset.availableCharsets().keySet()) {
            System.out.println(">> key: " + string + " , value: " + Charset.availableCharsets().get(string));
        }
    }
}
